echo "=== 安装docker ==="
apt update -y
apt install -y docker.io docker-compose
cat > /etc/docker/daemon.json << EOF
{
"insecure-registries":["172.21.100.16:9998"],
"data-root": "/data/docker",
"log-opts": {"max-size":"10m", "max-file":"1"},
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
EOF
systemctl restart docker

mkdir -p /data/wp
cd /data/wp
# 重新部署的话，清理文件
rm -f ~/.wpinstall_config

WP_PORT=8888
echo "wp_port:$WP_PORT" >> ~/.wpinstall_config
MYSQL_ROOT_PASSWORD=SuperRootPwd
MYSQL_DATABASE=wordpress
MYSQL_USER=wpuser
MYSQL_PASSWORD=wppass
# 传递配置，恢复时用
echo "MYSQL_ROOT_PASSWORD:${MYSQL_ROOT_PASSWORD}" >> ~/.wpinstall_config
echo "MYSQL_DATABASE:${MYSQL_DATABASE}" >> ~/.wpinstall_config
echo "MYSQL_USER:${MYSQL_USER}" >> ~/.wpinstall_config
echo "MYSQL_PASSWORD:${MYSQL_PASSWORD}" >> ~/.wpinstall_config

### 创建docker-compose文件
cat > docker-compose.yaml << _EOF_
version: "3.8"

services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      # === 数据库账号 ===
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
      TZ: "Asia/Shanghai"
    volumes:
      - ./data/db:/var/lib/mysql   # 数据库存储
    ports:
      - "3306:3306"

  wordpress:
    image: wordpress:php7.4-apache  # 7.4
    depends_on:
      - db
    restart: always
    environment:
      # === WP 与 DB 连接 ===
      WORDPRESS_DB_HOST: "db:3306"
      WORDPRESS_DB_NAME: "${MYSQL_DATABASE}"
      WORDPRESS_DB_USER: "${MYSQL_USER}"
      WORDPRESS_DB_PASSWORD: "${MYSQL_PASSWORD}"
      WORDPRESS_TABLE_PREFIX: "wp_"
      # === 其他可选参数 ===
      WORDPRESS_DEBUG: "false"
      TZ: "Asia/Shanghai"
    volumes:
      - ./data/html:/var/www/html      # WordPress 站点文件
    ports:
      - "${WP_PORT}:80"
_EOF_

# 启动
docker-compose up -d

echo "=== 安装nginx ==="
apt install -y nginx jq unzip
sed -i '/^[[:space:]]*http[[:space:]]*{/a\        client_max_body_size 500m;' /etc/nginx/nginx.conf
systemctl restart nginx


echo "=== 安装安装acme.sh ==="
cd ~
#git clone --depth=1 https://gitee.com/neilpang/acme.sh.git
git clone --depth=1 https://github.com/acmesh-official/acme.sh.git
cd acme.sh

# 安装到 ~/.acme.sh 并写好 cron(or systemd)，-m 指定联系邮箱
./acme.sh --install -m "my@example.com"
# 软链接，方便脚本使用
ln -sf ~/.acme.sh/acme.sh /usr/local/bin/acme.sh
